package com.bst.bsj.bops.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.AreaService;
import com.bst.bsj.bops.web.biz.MemberService;
import com.bst.bsj.bops.web.biz.SearchHisService;
import com.bst.bsj.bops.web.biz.SeedlingCompanyService;
import com.bst.bsj.bops.web.biz.SeedlingService;
import com.bst.bsj.bops.web.biz.UserVisitService;
import com.bst.bsj.common.dal.dataobject.Area;
import com.bst.bsj.common.maopu.enums.CompanyAuthStatus;
import com.bst.bsj.common.maopu.enums.SearchHisStatusEnum;
import com.bst.bsj.portal.util.StringUtil;

/**
* @Title: BopsSysController.java
* @Package com.bst.bsj.bops.web.controller
* @Description: 此中方法，只验证环境和登录，不验证菜单权限
* @author yj
* @date 2016年4月8日 下午3:53:38
* @version V1.0
* Copyright (c) 2015, 博思堂博视界.
 */
@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/base")
public class BopsSysController extends WebBaseController {

  @Autowired
  private MemberService memberService;

  @Autowired
  private SeedlingCompanyService seedlingCompanyService;

  @Autowired
  private SeedlingService seedlingService;

  @Autowired
  private AreaService areaService;

  @Autowired
  private SearchHisService searchHisService;
  
  @Autowired
  private UserVisitService userVisitService;

  @RequestMapping(value = "/main")
  public ModelAndView main(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String menuStr = (String) req.getSession().getAttribute("urlList");
    String uri = req.getRequestURI();
    if (!menuStr.contains(uri)) {
      JSONArray menuArr=JSONArray.parseArray(menuStr);
      return new ModelAndView(new RedirectView(menuArr.get(1).toString()));
    }
    
    ModelAndView modelAndView = getBopsModelAndView("data");
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("authStatus", CompanyAuthStatus.P.getCode());
    Long allAuthCount = seedlingCompanyService.queryCount(paramMap);
    modelAndView.addObject("allAuthCount", allAuthCount); //企业认证个数

    List<Area> areaList = areaService.getAreas("province");
    modelAndView.addObject("areaList", areaList); //所在地

    List<Map<String, Object>> memList = getMemberData(req, resp);
    modelAndView.addObject("memList", memList); //注册用户数据

    List<Map<String, Object>> authList = getAuthData(req, resp);
    modelAndView.addObject("authList", authList); //企业数据

    List<Map<String, Object>> catagoryList = getCategory(req, resp); //品种发布数据
    modelAndView.addObject("catagoryList", catagoryList);

    List<Map<String, Object>> seedlingTypeList = getType(req, resp); //种类发布数据
    modelAndView.addObject("seedlingTypeList", seedlingTypeList);

    List<Map<String, Object>> searchHisList = getSearchHis(req, resp); //搜索统计
    modelAndView.addObject("searchHisList", searchHisList);
    
    Long visit=getVisit(req, resp); //网站访问量
    modelAndView.addObject("visit", visit);
    
    return modelAndView;
  }

  /**
   * 
  * @Title: getMemberData
  * @Description: 获取注册用户数据
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Map<String,Object>>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月19日 上午12:57:41
  * @modificator :yj
  * @modify date :2016年5月19日 上午12:57:41
   */
  @RequestMapping(value = "/getMemberData")
  @ResponseBody
  public List<Map<String, Object>> getMemberData(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);
    paramsMap.put("sTime", params.get("sTime"));
    paramsMap.put("eTime", params.get("eTime"));
    paramsMap.put("province", params.get("province"));

    List<Map<String, Object>> merList = memberService.queryAll(paramsMap);

    return merList;
  }

  /**
   * 
  * @Title: getAuthData
  * @Description:企业数据
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Map<String,Object>>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月19日 上午12:56:22
  * @modificator :yj
  * @modify date :2016年5月19日 上午12:56:22
   */
  @RequestMapping(value = "/getAuthData")
  @ResponseBody
  public List<Map<String, Object>> getAuthData(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);
    paramsMap.put("sTime", params.get("sTime"));
    paramsMap.put("eTime", params.get("eTime"));
    paramsMap.put("province", params.get("province"));

    List<Map<String, Object>> authList = new ArrayList<Map<String, Object>>();
    List<Map<String, Object>> authNumList = seedlingCompanyService.queryAuthNum(paramsMap);
    Long cNum = 0l;
    Long sArea = 0l;
    Long allCNum = 0l;
    Long allSArea = 0l;
    for (int i = 0; i < authNumList.size(); i++) {
      Map<String, Object> map = (Map<String, Object>) authNumList.get(i);
      String authStatus = (String) map.get("auth_status");
      Object oCnum = map.get("cNum");
      Object oSarea = map.get("sArea");

      Long lCnum = 0l;
      Long lSarea = 0l;
      if (StringUtil.isNumber(oCnum + "")) {
        lCnum = Long.valueOf(oCnum + "");
      }
      if (StringUtil.isNumber(oSarea + "")) {
        lSarea = Long.valueOf(oSarea + "");
      }

      if (CompanyAuthStatus.P.getCode().equals(authStatus)) {
        map.put("auth_status", "认证用户");
        authList.add(map);
        allCNum += lCnum;
        allSArea += lSarea;
      } else {
        cNum += lCnum;
        sArea += lSarea;
      }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("auth_status", "非认证用户");
    map.put("cNum", cNum);
    map.put("sArea", sArea);
    authList.add(map);

    map = new HashMap<String, Object>();
    map.put("auth_status", "全部用户");
    map.put("cNum", allCNum + cNum);
    map.put("sArea", allSArea + sArea);
    authList.add(map);

    return authList;
  }

  /**
   * 
  * @Title: getCategory
  * @Description:品种发布数据
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Object>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月19日 上午12:05:12
  * @modificator :yj
  * @modify date :2016年5月19日 上午12:05:12
   */
  @RequestMapping(value = "/getCategory")
  @ResponseBody
  public List<Map<String, Object>> getCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();

    Map<String, Object> params = getBopsParams(req);
    paramsMap.put("sTime", params.get("sTime"));
    paramsMap.put("eTime", params.get("eTime"));
    paramsMap.put("province", params.get("province"));
    List<Map<String, Object>> catagoryList = seedlingService.getCategory(paramsMap);

    return catagoryList;
  }

  /**
   * 
  * @Title: getType
  * @Description: 种类发布数据
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Map<String,Object>>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月19日 上午12:47:10
  * @modificator :yj
  * @modify date :2016年5月19日 上午12:47:10
   */
  @RequestMapping(value = "/getType")
  @ResponseBody
  public List<Map<String, Object>> getType(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);
    paramsMap.put("sTime", params.get("sTime"));
    paramsMap.put("eTime", params.get("eTime"));
    paramsMap.put("province", params.get("province"));
    List<Map<String, Object>> catagoryList = seedlingService.getSeedlingType(paramsMap);

    return catagoryList;
  }

  /**
  * @Title: getSearch
  * @Description: 搜索排行
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Map<String,Object>>
  * @throws 
  * @creator     :yj
  * @create date :2016年5月21日 上午9:15:55
  * @modificator :yj
  * @modify date :2016年5月21日 上午9:15:55
   */
  @RequestMapping(value = "/getSearchHis")
  @ResponseBody
  public List<Map<String, Object>> getSearchHis(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);
    paramsMap.put("province", params.get("province"));
    paramsMap.put("status", SearchHisStatusEnum.NORMAL.getCode());
    List<Map<String, Object>> searchHisList = searchHisService.queryAll(paramsMap);

    return searchHisList;
  }

  /**
  * @Title: getVisit
  * @Description: 网站访问量
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * Long
  * @throws 
  * @creator     :yj
  * @create date :2016年5月30日 上午10:34:08
  * @modificator :yj
  * @modify date :2016年5月30日 上午10:34:08
   */
  @RequestMapping(value = "/getVisit")
  @ResponseBody
  public  Long getVisit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    Map<String, Object> params = getBopsParams(req);
    String sTime=(String)params.get("sTime");
    
    Calendar   cal   =   Calendar.getInstance();
    cal.add(Calendar.DATE,   -1);
    String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
    if(StringUtil.isEmpty(sTime)){
      sTime= yesterday+" 23:59:59";
    }
    paramsMap.put("sTime", sTime);
    paramsMap.put("eTime", params.get("eTime"));
    paramsMap.put("province", params.get("province"));
    
    return userVisitService.queryVisit(paramsMap);
  }
  
  
  /**
   * 
  * @Title: getMenuList
  * @Description:  获取角色的可用菜单
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * List<Resource>
  * @throws 
  * @creator     :yj
  * @create date :2016年3月30日 上午11:16:04
  * @modificator :yj
  * @modify date :2016年3月30日 上午11:16:04
   */
  @RequestMapping(value = "/menuList")
  @ResponseBody
  public List<Object> getMenuList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String str = (String) req.getSession().getAttribute("menuList");
    List<Object> menuList = JSON.parseArray(str);
    return menuList;
  }


  
}
